package hos.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

/**
 * <p>Title: AppCompatArrayAdapter </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2022/4/28 13:26
 */
public class CommonArrayAdapter<T> extends ArrayAdapter<T> {

    private OnItemCreateListener mOnItemCreateListener;
    private OnDropDownCreateListener mOnDropDownCreateListener;

    private int mTextColor = -1;

    private int mTextSize = -1;

    private int mTextDownColor = -1;

    private int mTextDownSize = -1;


    public CommonArrayAdapter(Context context, int resource) {
        super(context, resource);
    }

    public CommonArrayAdapter(Context context, int resource, int textViewResourceId) {
        super(context, resource, textViewResourceId);
    }

    public CommonArrayAdapter(Context context, int resource, T[] objects) {
        super(context, resource, objects);
    }

    public CommonArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public CommonArrayAdapter(Context context, int resource, List<T> objects) {
        super(context, resource, objects);
    }

    public CommonArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
        super(context, resource, textViewResourceId, objects);
    }

    public void setText(int textColor, int textSize) {
        setText(textColor, textSize, textColor, textSize);
    }

    public void setText(int textColor, int textSize, int textDownColor, int textDownSize) {
        setTextColor(textColor);
        setTextSize(textSize);
        setTextDownColor(textDownColor);
        setmTextDownSize(textDownSize);
    }

    public void setTextColor(int textColor) {
        this.mTextColor = textColor;
    }

    public int getTextColor() {
        return mTextColor;
    }

    public int getTextSize() {
        return mTextSize;
    }

    public void setTextSize(int textSize) {
        this.mTextSize = textSize;
    }

    public int getTextDownColor() {
        return mTextDownColor;
    }

    public void setTextDownColor(int textDownColor) {
        this.mTextDownColor = textDownColor;
    }

    public int getmTextDownSize() {
        return mTextDownSize;
    }

    public void setmTextDownSize(int textDownSize) {
        this.mTextDownSize = textDownSize;
    }

    public void setOnItemCreateListener(OnItemCreateListener mOnItemCreateListener) {
        this.mOnItemCreateListener = mOnItemCreateListener;
    }

    public void setOnDropDownCreateListener(OnDropDownCreateListener mOnDropDownCreateListener) {
        this.mOnDropDownCreateListener = mOnDropDownCreateListener;
    }

    public View getDefaultView(int position, View convertView, ViewGroup parent) {
        return super.getView(position, convertView, parent);
    }

    public View getDefaultDropDownView(int position, View convertView, ViewGroup parent) {
        return super.getDropDownView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (mOnItemCreateListener != null) {
            return mOnItemCreateListener.getView(this, position, convertView, parent);
        }
        View target = super.getView(position, convertView, parent);

        if (target instanceof TextView) {
            if (mTextColor != -1) {
                ((TextView) target).setTextColor(mTextColor);
            }
            if (mTextSize != -1) {
                ((TextView) target).setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize);
            }
        }
        return target;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if (mOnDropDownCreateListener != null) {
            return mOnDropDownCreateListener.getDropDownView(this, position, convertView, parent);
        }
        View target = super.getDropDownView(position, convertView, parent);
        if (target instanceof TextView) {
            if (mTextDownColor != -1) {
                ((TextView) target).setTextColor(mTextDownColor);
            }
            if (mTextDownSize != -1) {
                ((TextView) target).setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextDownSize);
            }
        }
        return target;
    }

    public interface OnItemCreateListener {
        View getView(CommonArrayAdapter<?> adapter, int position, View convertView, ViewGroup parent);
    }

    public interface OnDropDownCreateListener {
        View getDropDownView(CommonArrayAdapter<?> adapter, int position, View convertView, ViewGroup parent);
    }


    public void onDestroy() {
        mOnItemCreateListener = null;
        mOnDropDownCreateListener = null;
    }
}
